package com.fieldchain.ex.fa100.ui.trade.fragment;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.bean.EntiustBean;
import com.fieldchain.ex.fa100.ui.trade.adapter.TradHistoryAdapter;
import com.fieldchain.ex.fa100.utils.screen.MyToast;
import com.fieldchain.ex.fa100.utils.share.ShareManager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.modle.ListRequestTwo;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/20
 */
public class TradHistoryFragment extends BaseTradFragment {
    private SmartRefreshLayout refresh;
    private RelativeLayout reaEmpty;
    private TextView tvEmpty;
    private SwipeMenuRecyclerView rv;
    private int index=1;
    private int page = 1;
    private List<EntiustBean> mList = new ArrayList<>();
    private TradHistoryAdapter adapter;
    @Override
    protected void findView() {
        refresh = view.findViewById(R.id.refresh);
        reaEmpty = view.findViewById(R.id.rea_empty);
        tvEmpty = view.findViewById(R.id.tv_empty);
        rv = view.findViewById(R.id.rv);
    }

    @Override
    protected void setValue() {
        initRefresh();
        initRv();
    }


    private void initRv() {
        tvEmpty.setText("暂无记录");
        adapter=new TradHistoryAdapter(mContext);
        rv.setLayoutManager(new LinearLayoutManager(mContext));
        rv.setAdapter(adapter);
    }

    @Override
    public int getLayout() {
        return R.layout.fragment_trad_history;
    }

    @Override
    public void setCompany() {
        mList.clear();

        getData();
    }



    public void initRefresh() {
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                refreshLayout.finishLoadMore(1000);
                getData();
            }
        });

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                mList.clear();
                refresh.finishRefresh(1000);
                getData();
            }
        });
    }

    public void getData() {
        Log.e("tradHistory","getData");
        HashMap map = new HashMap();
        map.put("page", page + "");
        map.put("market",market.toLowerCase());
        map.put("status",-1+"");
        HttpRequest.getInstance().getRequest(map, Constant.entiust, new ListRequestTwo<EntiustBean>() {
            @Override
            public void onSucess(List<EntiustBean> list, int code, String msg) {
                if (code == 200) {
                    mList.addAll(list);
                    adapter.setData(mList);
                }
                if (list.size() == 0) {
                    if (page > 1) {
                        page--;
                    }
                }
                if (mList.size() > 0) {
                    reaEmpty.setVisibility(View.GONE);
                } else {
                    reaEmpty.setVisibility(View.VISIBLE);
                }
            }
            @Override
            public void onFaild(Exception e) {
                if (page > 1) {
                    page--;
                }
                if (mList.size() > 0) {
                    reaEmpty.setVisibility(View.GONE);
                } else {
                    reaEmpty.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    public void loginShow() {
        if(ShareManager.getInstance().getUser()==null) {
            MyToast.show("未登录");
        }else{
            MyToast.show("登录过期");
        }
    }
}
